@extends('admin.basev2')

@section('content')
    <style>
        .layui-table-cell {
            height: inherit;
        }
    </style>
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                @can('changzu.renter.destroy')
                    <!--<button class="layui-btn layui-btn-sm layui-btn-danger" id="listDelete">删 除</button>-->
                @endcan
                @can('changzu.renter.create')
                    <a class="layui-btn layui-btn-sm" href="{{route('admin.renter.create')}}">添 加</a>
                @endcan
            </div>
            <div class="layui-form">
                <!--
                <div class="layui-input-inline" @if (!empty($citycode)) style="display: none"; @endif>
                    <select name="city_code" lay-search="" id="city_code" >
                        <option value="0">搜索/选择城市</option>
                        @foreach($citys as $ProvinceName=>$v)
                            <optgroup label="{{$v['province']['name']}}">
                                @foreach($v['city'] as $c)
                                    <option value="{{$c['code']}}" @if ( $c['code'] == $citycode) ' selected="selected"' @endif>{{$c['name']}}</option>
                                @endforeach
                            </optgroup>
                        @endforeach
                    </select>
                </div>

                <div class="layui-input-inline">
                    <select name="device_type" lay-search="" id="device_type">
                        <option value="0">设备类型</option>
                        <option value="1">门禁</option>
                        <option value="2">道闸</option>
                        <option value="3">灯箱</option>
                    </select>
                </div>-->
                <div class="layui-input-inline">
                    <input type="text" name="keyword" id="keyword" placeholder="场租方名称" class="layui-input">
                </div>
                <button class="layui-btn" id="searchBtn">搜 索</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <!--
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('houses.property.show')
                        <a class="layui-btn layui-btn-sm" lay-event="show">查看</a>
                    @endcan
                    @can('changzu.renter.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    @endcan
                    @can('changzu.renter.destroy')
                    @endcan
                </div>
            </script>
            -->
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm more" lay-event="more">更多 <i class="layui-icon layui-icon-down"></i></a>

                </div>
            </script>
            <script type="text/html" id="contracturl">
                @{{#  if(d.contracturl){ }}
                <a href="@{{d.contracturl}}" target="_blank" title="点击查看">
                    <img src="@{{d.contracturl}}" alt="" width="28" height="28">
                </a>
                @{{#  } }}
            </script>

            <script type="text/html" id="shuidian">
                <input id="id" value="@{{d.id}}" hidden>
                <a href="/admin/renter/weList?renter_id=@{{d.id}}" title="点击查看">查看</a>
            </script>

            <script type="text/html" id="shouyi">
                <a href="#" title="点击查看" onclick="shouyi();">查看</a>
            </script>

        </div>
    </div>
@endsection

@section('script')
@can('changzu.renter')
<script>
layui.use(['layer', 'table', 'form','dropdown'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var dropdown = layui.dropdown;
    //用户表格初始化
    var dataTable = table.render({
        elem: '#dataTable'
        , autoSort: false
        , height: "auto"
        //, toolbar: '#search'
        //, defaultToolbar: ['filter']
        , url: "{{ route('admin.renter.data') }}" //数据接口
        , page: true //开启分页
        , cols: [[ //表头
            //{checkbox: true, fixed: true}
            {field: 'id', title: 'ID', sort: true, width: 60}
            , {field: 'name', title: '场租方'}
            , {field: 'operation_type', title: '合作类型',templet:function (d){
                    if (d.contract != ""){
                        if (d.contract[0].operation_type == 0){
                            return '自运营';
                        }else if (d.contract[0].operation_type == 1){
                            return '合作运营A';
                        }else if (d.contract[0].operation_type == 2){
                            return '合作运营B';
                        }
                    }else{
                        //return '待补充数据';
                        return '-';
                    }
                }}
            , {field: 'rente_price', title: '场租',templet:function(d){
                if (d.contract != ""){
                    return d.contract[0].rente_price;
                }else{
                    return '0';
                }
            }}
            , {field: 'shuidian', title: '月度费用', toolbar:'#shuidian'}
            , {field: 'device_num', title: '机器数量',templet:function (d){
                    if (d.device_count !==  undefined) return d.device_count; else return 0;
                }}
            , {field: 'shouyi', title: '收益分配', toolbar:'#shouyi'}
            , { title: '合作日期',width: 110, templet:function (d){
                    if (d.contract != ""){
                        return d.contract[0].starttime.substring(0,10)+" <br> " +d.contract[0].endtime.substring(0,10);
                    }else{
                        return '';
                    }
                }}
            , {field: 'zhuangtai', title: '状态',templet:function (d){
                    var date = new Date();
                    //提前一个月提醒，合作即将到期
                    var yy1 = date.getFullYear();
                    //当前日期
                    var mm2 = date.getMonth() + 1;
                    var dd2 = date.getDate();
                    
                    if (mm2 < 10 ) mm2 = '0' + mm2;
                    if (dd2 < 10)  dd2 = '0' + dd2;
                    var cdate = yy1+"-"+mm2+"-"+dd2;

                    var contract_begin = '';
                    var contract_end = '';
                    if (d.contract != "") contract_begin = d.contract[0].starttime;
                    if (d.contract != "") contract_end = d.contract[0].endtime;

                    if (cdate >= contract_begin && cdate <= contract_end ){
                        return '合作中';
                    }else{
                        return '未合作';
                    }
                }}
            , {fixed: 'right', align: 'center', width: 110,toolbar: '#options'}
        ]]
    });
    //监听工具条
    table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            ,datac = obj.data
            , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'del') {
            layer.confirm('确认删除吗？', function (index) {
                layer.close(index);
                var load = layer.load();
                $.post("{{ route('admin.renter.destroy') }}", {
                    _method: 'post',
                    ids: [data.id]
                }, function (res) {
                    layer.close(load);
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            obj.del();
                        })
                    } else {
                        layer.msg(res.msg, {icon: 2})
                    }
                });
            });
        } else if (layEvent === 'edit') {
            location.href = '/admin/property/edit?id=' + data.id;
            // top.layui.index.openTabsPage('/admin/device/edit?id=' + data.id, '编辑设备');
        }else if (layEvent === "more"){
            dropdown.render({
                elem: this
                ,show: true //外部事件触发即显示
                ,data: [
                    @can('changzu.renter.edit')
                    {
                        title: '查看合同'
                        ,id: 'contract'
                    },{
                        title: '站点信息'
                        ,id: 'sitelist'
                    }
                    @endcan
                    ]
                ,click: function(data, othis){
                    @can('changzu.renter.edit')
                    if (data.id === "contract") {
                        top.layui.index.openTabsPage('/admin/renter/contract?renter_id=' + datac.id, '合同信息');
                    }
                    if (data.id === "sitelist") {
                        top.layui.index.openTabsPage('/admin/site?renter_id=' + datac.id, '站点列表');
                    }
                    @endcan
                }
                ,style: 'margin-left: -45px; box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
            });
        }
    });

    //按钮批量删除
    $("#listDelete").click(function () {
        var ids = [];
        var hasCheck = table.checkStatus('dataTable');
        var hasCheckData = hasCheck.data;
        if (hasCheckData.length > 0) {
            $.each(hasCheckData, function (index, element) {
                ids.push(element.id)
            })
        }
        if (ids.length > 0) {
            layer.confirm('确认删除吗？', function (index) {
                layer.close(index);
                var load = layer.load();
                $.post("{{ route('admin.renter.destroy') }}", {
                    _method: 'POST',
                    ids: ids
                }, function (res) {
                    layer.close(load);
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            dataTable.reload({page: {curr: 1}});
                        })
                    } else {
                        layer.msg(res.msg, {icon: 2})
                    }
                });
            });
        } else {
            layer.msg('请选择删除项', {icon: 2})
        }
    })

    //搜索
    $("#searchBtn").click(function () {
        var keyword = $("#keyword").val();
        dataTable.reload({
            where: {keyword: keyword},
            page: {curr: 1}
        })
    })
})
</script>
@endcan
@endsection
